/* 
 * File:   main.cpp
 * Author: H4314
 *
 * Created on 24 avril 2012, 08:13
 */

#include <cstdio>
#include <string>
#include <stdlib.h>

#include "../xml/XMLParse.h"
#include "../dtd/DTDParse.h"
#include "StyleSheet.h"
#include "Template.h"
#include "../xml/Attribute.h"

extern int xmldebug;
using namespace std;

int main(int argc, char** argv) {
    
    char * c_default_dtd_path;
    Element * dataSheetRoot;
    Element * xmlRoot;
    xmlin = NULL;
    
    if(argc != 3) {
	    cout << "Usage : ./xslout file.xml file.xsl" << endl;
	    return 1;
    }
    xmlin = fopen(argv[2], "r");
    if (xmlin == NULL) {
        cout << "Le fichier XSL est introuvable." << endl;
        exit(1);
    }
    
    xmldebug = 0;
    if(xmlparse(&dataSheetRoot, &c_default_dtd_path) == 0)
    {
        // Construction de la feuille de style
        StyleSheet st(dataSheetRoot);
        cout << st;
        
        xmlin = fopen(argv[1], "r");
        if (xmlin == NULL) {
            cout << "Le fichier XML est introuvable." << endl;
            exit(1);
        }
        
        xmldebug = 0;
        list<Node *> newNodeList;
        if(xmlparse(&xmlRoot, &c_default_dtd_path) == 0)
        {
            cout << "\033[33m__________Applied transformations__________\033[00m" << endl;

            list<Node *>::const_iterator it;
            // Construction de l'arbre html
            newNodeList = st.Transform(xmlRoot);
            
            // Affichage de l'arbre html
            for(it = newNodeList.begin(); it != newNodeList.end(); it++)
                (*it)->Draw(cout, 0, true);
            
        }
        cout << "\033[33m__________Free documents__________\033[00m" << endl;
        cout << Node::NODES << " nodes to delete." << endl;
        cout << Attribute::ATTRS << " attributes to delete." << endl;
        Node::CleanItUp(newNodeList);// Libération mémoire newNodeList
        delete dataSheetRoot;// Libération mémoire datasheet
        delete xmlRoot;// Libération mémoire xml
        
    }// Libération mémoire StyleSheet st
    
    cout << Node::NODES << " nodes remains." << endl;
    cout << Attribute::ATTRS << " attributes remains." << endl;
    return 0;
}

